from aiogram import Bot, Dispatcher, Router, types
import asyncio
import sys
import os
from aiogram.enums import ParseMode
from aiogram.types import Message, BufferedInputFile
from aiogram.filters import CommandStart
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from crud import crud
import io
from config.config import Config

config = Config("config/config.yaml")
TOKEN = str(config.get("bot").get("token"))
dp = Dispatcher()
bot = Bot(token=TOKEN)
chat_id = str(config.get("bot").get("chat_id"))


@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
    await message.answer(f"Привет!\nЭтот бот будет слать порнуху раз в час.")

async def scheduled_send_image():
    while True:
        photo_path: str = await crud.get_image()
        with open(photo_path, 'rb') as f:
            buffer = io.BytesIO(f.read())
            data = buffer.getvalue()
            await bot.send_photo(chat_id, BufferedInputFile(data, filename=photo_path))
            await asyncio.sleep(3600)  # Пауза в 3600 секунд (1 час)
            if os.path.isfile(photo_path):
                os.remove(os.path.join(photo_path))  

async def scheduler():
    await scheduled_send_image()
 
async def main():
    loop = asyncio.get_event_loop()
    loop.create_task(scheduler())
    await dp.start_polling(bot)

if __name__ == "__main__":
    asyncio.run(main())




